package com.cutefool.zero.code.admin.meta.column.web;

import com.cutefool.core.res.PageResponse;
import com.cutefool.core.res.Response;
import com.cutefool.dsl.mybatis.core.dsl.builder.DeleteDsl;
import com.cutefool.dsl.mybatis.core.dsl.builder.QueryDsl;
import com.cutefool.dsl.mybatis.core.dsl.condition.Conditioning;
import com.cutefool.dsl.mybatis.core.dsl.operation.CuteCurdOperation;
import com.cutefool.dsl.mybatis.core.page.Paging;
import com.cutefool.zero.code.admin.consts.Constants;
import com.cutefool.zero.code.admin.meta.column.dto.ColumnDTO;
import com.cutefool.zero.code.admin.meta.column.dto.ColumnValidateDTO;
import com.cutefool.zero.code.admin.meta.column.service.ColumnService;
import com.cutefool.zero.code.core.domain.ModelColumn;
import com.cutefool.zero.code.core.objects.ValidateDTO;
import jakarta.annotation.Resource;
import org.mybatis.dynamic.sql.SqlBuilder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping(Constants.API_PREFIX)
public class ColumnController {

    @Resource
    private ColumnService columnService;

    @Resource
    private CuteCurdOperation<ModelColumn> cuteCurdOperation;

    /**
     * 新增
     *
     * @param dto dto
     * @return Response<Long>
     **/
    @PostMapping("/add/model-columns")
    public Response<?> create(@RequestBody @Validated ColumnDTO dto) {
        return this.columnService.create(dto);
    }

    /**
     * 校验字段名称
     *
     * @param dto dto
     * @return Response<Long>
     **/
    @PostMapping("/validate/model-column-names")
    public Response<?> validateNames(@RequestBody @Validated ColumnValidateDTO dto) {
        boolean b = this.columnService.validateName(dto.getId(), dto.getTableName(), dto.getName());
        return Response.ok(b);
    }

    /**
     * 校验显示名称
     *
     * @param dto dto
     * @return Response<Long>
     **/
    @PostMapping("/validate/model-column-display-names")
    public Response<?> validateTableName(@RequestBody @Validated ColumnValidateDTO dto) {
        boolean b = this.columnService.validateDisplayName(dto.getId(), dto.getTableName(), dto.getName());
        return Response.ok(b);
    }

    /**
     * 删除
     *
     * @param id id
     * @return Response<Boolean>
     **/
    @PostMapping("/delete/model-columns/{id}")
    public Response<Boolean> delete(@PathVariable Long id) {
        DeleteDsl<ModelColumn> deleteDsl = DeleteDsl.newInstance(ModelColumn.instance(), (where) -> where.and(ModelColumn.instance().id(), SqlBuilder.isEqualTo(id)));
        this.cuteCurdOperation.delete(deleteDsl);
        return Response.ok();
    }


    /**
     * 查询（通过ID）
     *
     * @param id id
     * @return Response<Model>
     **/
    @GetMapping("/info/model-columns/{id}")
    public Response<ModelColumn> byId(@PathVariable Long id) {
        QueryDsl<ModelColumn> queryDsl = QueryDsl.newInstance(ModelColumn.instance(), where -> where.and(ModelColumn.instance().id(), SqlBuilder.isEqualTo(id)));
        Optional<ModelColumn> optional = this.cuteCurdOperation.get(queryDsl);
        return Response.ok(optional.orElse(null));
    }

    /**
     * 查询（列表）
     *
     * @param query query
     * @return Response<List < Model>>
     **/
    @GetMapping("/page/model-columns")
    public Response<PageResponse<ModelColumn>> page(Conditioning query, Paging paging) {
        QueryDsl<ModelColumn> queryDsl = QueryDsl.newInstance(ModelColumn.instance(), query, paging);
        PageResponse<ModelColumn> entities = this.cuteCurdOperation.page(queryDsl);
        return Response.ok(entities);
    }
}
